//
//  LRRefreshControl.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/23.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit
// 自定义刷新控件.里面会包含一个自定的view
class LRRefreshControl: UIRefreshControl {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init() {
        super.init()
        addSubview(refreshView)
        //添加约束
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: self.bounds.size)
    }
    
    // 定义刷新控件偏移值
    private let refreshControlOffY: CGFloat = -60
    // 定义一个标示,记录箭头是否朝上
    private var isUp = false
    // 覆盖父类的属性,监听frame的改变
    override var frame: CGRect {
        didSet{
            //print("RefreshControlFrame: \(frame)")
            // 1.origin.y大于0,看不到刷新控件
            if frame.origin.y > 0 {
                return
            }
            // 判断refreshing 是否正在刷新
            if refreshing {
                print("正在刷新数据")
                refreshView.startLoading()
            }
            // 小于 -60,之前箭头需要朝下, 箭头转上去
            if frame.origin.y < refreshControlOffY && !isUp{
                isUp = !isUp
                print("箭头转上去动画")
                refreshView.rotationTipImageView(isUp)
            }else if frame.origin.y > refreshControlOffY && isUp {// 0 到 -60之间,之前箭头需要朝上
                isUp = !isUp
                print("箭头转上去动画")
                refreshView.rotationTipImageView(isUp)
            }
            
        }
    }
    /// 重写父类的方法.自己来做额外的事情
    override func endRefreshing() {
        super.endRefreshing()
        print("结束刷新")
        refreshView.endLoading()
    }
    //懒加载下拉刷新view
    lazy var refreshView: LRRefreshView = LRRefreshView.refreshView()
}

//自定义LRRefreshView 与xib关联
class LRRefreshView: UIView {
    
    //圆圈指示器
    @IBOutlet weak var circleImageView: UIImageView!
    //下拉刷新提示view
    @IBOutlet weak var tipView: UIView!
    //下拉箭头
    @IBOutlet weak var tipImageView: UIImageView!
    //MARK: - 类方法返回加载的xib
    class func refreshView() -> LRRefreshView {
        return NSBundle.mainBundle().loadNibNamed("LRRefreshView", owner: nil, options: nil).last as! LRRefreshView
    }
    //MARK: - 箭头旋转
    func rotationTipImageView(isUp: Bool) {
        UIView.animateWithDuration(0.25) { () -> Void in
            self.tipImageView.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
    }
    // MARK: - 菊花旋转
    func startLoading() {
        let animationKey = "animationKey"
        // 1.判断动画是否正在执行,如果正在执行,直接返回
        if circleImageView.layer.animationForKey(animationKey) != nil {
            // 表示动画正在执行
            print("动画正在执行,什么都不干")
            return
        }
        // 2. 隐藏tipView
        tipView.hidden = true
        //旋转圆圈
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 0.25
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleImageView.layer.addAnimation(animation, forKey: animationKey)
    }
    // MARK: - 结束加载
    func endLoading() {
        //隐藏下拉刷新view
        tipView.hidden = false
        circleImageView.layer.removeAllAnimations()
    }
  
}
